/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.util;

import com.comac.ins.isfd.constant.enums.IsfdDatasourceTaskSchedulingConfigCycleEnum;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 时间工具类
 */
public class DateUtils {

    /**
     * 将时间段切分成多个时间点
     */
    public static List<Date> getTimePoints(Date startTime, Date endTime, Long schedulingCycle, String cycleUnit) {
        List<Date> timePoints = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);

        while (!calendar.getTime().after(endTime)) {
            timePoints.add(calendar.getTime());
            if (IsfdDatasourceTaskSchedulingConfigCycleEnum.HOUR.getValue().equals(cycleUnit)) {
                calendar.add(Calendar.HOUR_OF_DAY, Math.toIntExact(schedulingCycle));
            } else if (IsfdDatasourceTaskSchedulingConfigCycleEnum.DAY.getValue().equals(cycleUnit)) {
                calendar.add(Calendar.DAY_OF_MONTH, Math.toIntExact(schedulingCycle));
            }
        }

        return timePoints;
    }

    /**
     * 获取指定日期范围内的所有月份
     */
    public static List<String> getAllMonthsBetween(Date startTime, Date endTime) {
        List<String> months = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        while (!start.after(end)) {
            months.add(new SimpleDateFormat("yyyy-MM").format(start.getTime()));
            start.add(Calendar.MONTH, 1);
        }
        return months;
    }

    /**
     * 查找最近的且大于 nowTime 的时间点
     */
    public static Date findNearestLaterTimePoint(List<Date> timePoints, Date nowTime) {
        return timePoints.stream()
            .filter(timePoint -> timePoint.after(nowTime))
            .min(Date::compareTo)
            .orElse(null); // 如果没有找到大于 nowTime 的时间点，则返回 null
    }

    public static void main(String[] args) {
        // 示例使用
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date startTime = sdf.parse("2023-10-01 11:59:59");
            Date endTime = sdf.parse("2023-10-01 12:00:58");
            Date nowTime = sdf.parse("2023-10-12 14:30:00");
            Long schedulingCycle = 1L;
            String cycleUnit = "day";
            List<Date> timePoints = getTimePoints(startTime, endTime, schedulingCycle, cycleUnit);
            System.out.println("Time Points: " + timePoints.stream()
                .map(sdf::format)
                .collect(Collectors.joining(", ")));

            Date nearestLaterTimePoint = findNearestLaterTimePoint(timePoints, nowTime);
            if (nearestLaterTimePoint != null) {
                System.out.println("Nearest later now point: " + sdf.format(nearestLaterTimePoint));
            } else {
                System.out.println("No time point found after the now time.");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
